/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Controleur;

import Modele.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author sio
 */
@WebServlet(name = "Control", urlPatterns = {"/Control"})
public class Control extends HttpServlet {

    private Modele.Pays p;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        p = new Pays();
    }

    public void destroy() {
        Connect.close();
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uc = request.getParameter("uc");
        String departement = request.getParameter("departement");
        String Dnom = request.getParameter("Dnom");
        String spe=request.getParameter("spe");
        String page;
        if(uc==null){
            uc="default";
        }
        switch (uc) {

            case "nom":
                if (Dnom == null) {
                    page = "nom.jsp";
                } else {
                    Collection<Med> lesMed ;
                    Collection<Med> lesVraiMed = new TreeSet<Med>();
                    Collection<Dep> lesDep0 = p.getLesDeps();
                    for (Dep unDep : lesDep0) {

                        lesMed = unDep.getLesMeds();
                        for (Med unMed : lesMed) {
                            String nomMed = unMed.getNom();
                            if (nomMed.startsWith(Dnom)) {
                                lesVraiMed.add(unMed);
                            }
                        }
                    }

                    page = "vrainom.jsp";
                    request.setAttribute("MedByNom", lesVraiMed);
                }
                
                break;

            case "dep":
                if (departement == null) {
                    Collection<Dep> lesDep = p.getLesDeps();
                    request.setAttribute("listeDep", lesDep);
                    page = "dep.jsp";
                } else {
                    Dep depart = p.getLeDep(departement);
                    Collection<Med> lesMeds = depart.getLesMeds();
                       request.setAttribute("listeMed", lesMeds);
                    page = "medByDep.jsp";
                 
                }
                break;
            case "spe": 
                if(spe==null){
                    Collection <Specialite> lesSpe=p.getLesspe();
                    request.setAttribute("listeSpe",lesSpe);
                    page="spe.jsp";
                }else{
                    Specialite speci;
                    speci = p.getLaSpe(spe);
                    Collection <Med> lesMeds = speci.getLesMeds(); 
                       request.setAttribute("listeMed", lesMeds);
                    page = "medBySpe.jsp";
                }
                
                
                    break;
            default:
                page = "index.jsp";

                break;
        }
        request.getRequestDispatcher(page).forward(request, response);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
